package com.yoo.ons.dialog

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.gyf.immersionbar.ImmersionBar
import com.yoo.module_base.R
import com.yoo.module_base.base.BaseDialogFragment
import com.yoo.module_base.view.CustomVerticalIndicator
import com.yoo.ons.adapter.ProfileInfoImageAdapter
import com.yoo.ons.databinding.DialogFragmentProfileBannerBinding
import com.yoo.ons.dialog.viewmodel.ProInfoDialogViewModel
import com.yoo.ons.function.like.viewmodel.WhoLikeMeViewModel
import com.yoo.ons.function.me.like.MyLikeListViewModel
import com.yoo.ons.function.me.viewmodel.MeViewModel
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.util.BannerUtils

/**
 * 带banner的信息弹窗
 * wml/im/i like显示
 */
class ProfileInfoBannerDialogFragment : BaseDialogFragment() {
    private var innerBinding: DialogFragmentProfileBannerBinding? = null
    private val binding get() = innerBinding!!
    private val userCode by lazy { arguments?.getString(PROFILE_INFO_USER_CODE)!! }
    private val openType by lazy { arguments?.getInt(PROFILE_INFO_OPEN_TYPE)!! }
    private val meViewModel by lazy {
        ViewModelProvider(requireActivity())[MeViewModel::class.java]
    }
    private val wlmViewModel by lazy {
        ViewModelProvider(requireActivity())[WhoLikeMeViewModel::class.java]
    }
    private val myLikeListViewModel by lazy {
        ViewModelProvider(requireActivity())[MyLikeListViewModel::class.java]
    }
    private val proInfoDialogViewModel by lazy {
        ViewModelProvider(requireActivity())[ProInfoDialogViewModel::class.java]
    }
    private var profileInfoDialog: ProfileInfoDialogFragment? = null
    override fun setWindow(win: Window) {
        win.setWindowAnimations(R.style.DialogAnimation)
        win.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        win.attributes = lp
        win.setGravity(Gravity.BOTTOM)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding = innerBinding
            ?: DialogFragmentProfileBannerBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).statusBarDarkFont(false).init()
    }

    private fun initData() {
        profileInfoDialog = ProfileInfoDialogFragment.newInstance(userCode, openType)
        profileInfoDialog?.isCancelable = false
        profileInfoDialog?.show(childFragmentManager, "ProfileInfoDialogFragment")
        val context = activity ?: return
        meViewModel.closeDialogLiveData.observe(viewLifecycleOwner) {
            //接收到ProfileInfoDialogFragment点击物理返回按钮
            if (it) {
                dismissAllowingStateLoss()
            }
        }
        wlmViewModel.likeTypeLiveData.observe(viewLifecycleOwner) {
            //收到点击喜欢和不喜欢关闭此弹窗
            if (it > 0) {
                dismissAllowingStateLoss()
            }
        }
        myLikeListViewModel.likeTypeLiveData.observe(viewLifecycleOwner) {
            //收到来自i like页面点击的fc
            if (it > 0) {
                dismissAllowingStateLoss()
            }
        }
        proInfoDialogViewModel.slideUpLiveData.observe(viewLifecycleOwner) {
            if (it == 0) {
                //上滑切换下一张
                if (binding.bannerProfileInfo.itemCount > 1) {
                    binding.bannerProfileInfo.currentItem += 1
                }
            } else if (it == 1) {
                //下滑切换上一张
                if (binding.bannerProfileInfo.itemCount > 1) {
                    binding.bannerProfileInfo.currentItem -= 1
                }
            }
        }
        meViewModel.userInfoLiveData.observe(viewLifecycleOwner) {
            if (it == null) {
                //会走两次、影响体验效果，所以在销毁的时候置为空
                return@observe
            }
            val linearLineIndicator = CustomVerticalIndicator(context)
            //banner显示根据type
            if (it.images.isNotEmpty()) {
                if (it.images.size > 1) {
                    linearLineIndicator.setIndicatorCount(it.images.size)
                    binding.bannerProfileInfo.indicator = linearLineIndicator
                    binding.bannerProfileInfo.setIndicatorMargins(
                        IndicatorConfig.Margins(
                            0, 0, BannerUtils.dp2px(20f), 0
                        )
                    )
                }
                binding.bannerProfileInfo.setAdapter(
                    ProfileInfoImageAdapter(
                        context,
                        it.images.toList()
                    )
                )
                binding.bannerProfileInfo.viewPager2.registerOnPageChangeCallback(object :
                    ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        linearLineIndicator.setCurrentIndex(position)
                        super.onPageSelected(position)
                    }
                })
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        //一定要修改这个值，要不然二次打开就先走observe
        meViewModel.closeProfileDialog(false)
        wlmViewModel.clickLikeType(-1)
        myLikeListViewModel.clickFcType(-1)
        profileInfoDialog?.dismissAllowingStateLoss()
        meViewModel.userInfoLiveData.value = null
        innerBinding = null
        dismissAllowingStateLoss()
    }

    companion object {
        private const val PROFILE_INFO_USER_CODE = "PROFILE_INFO_USER_CODE"
        private const val PROFILE_INFO_OPEN_TYPE = "PROFILE_INFO_OPEN_TYPE"
        fun newInstance(userCode: String, openType: Int): ProfileInfoBannerDialogFragment {
            return ProfileInfoBannerDialogFragment().apply {
                arguments = Bundle().apply {
                    putString(PROFILE_INFO_USER_CODE, userCode)
                    putInt(PROFILE_INFO_OPEN_TYPE, openType)
                }
            }
        }
    }
}